// 
// ThreadWorkItem.cs
//  
// Author:
//       Tomas Vondracek <tomas.vondracek@gmail.com>
// 
// Copyright (c) 2009 Tomas Vondracek
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;

namespace ZVI.Threading.Tasks
{
	public class GuiTask : AsyncTask
	{					
		private WorkCallback work;
		
		private bool isGuiWork;
		private System.Windows.Forms.Control control;

		public bool IsGuiWork {
			get {
				return isGuiWork;
			}
			set {
				isGuiWork = value;
			}
		}

		public WorkCallback Work {
			get {
				return work;
			}
		}

		public System.Windows.Forms.Control Control {
			get {
				return control;
			}
			set {
				control = value;
			}
		}
		
		/// <summary>
		/// creates work instance, takes only delegate
		/// </summary>
		/// <param name="work">
		/// A <see cref="WorkCallback"/>
		/// </param>
		public GuiTask (WorkCallback work)
		{
			this.work = work;
		}
		
		/// <summary>
		/// creates work instance with delegate and description
		/// </summary>
		/// <param name="description">
		/// A <see cref="System.String"/>
		/// </param>
		/// <param name="work">
		/// A <see cref="WorkCallback"/>
		/// </param>
		public GuiTask (string description, WorkCallback work)
		{
			this.work = work;
			this.description = description;
		}
		
		public override void Run ()
		{
			try
			{
				work ();
			}
			catch (Exception ex)
			{
				Logging.Logger.AddError ("ThreadWorkItem" + ex.ToString ());
			}
			
			Finished ();
		}
		
		/// <summary>
		/// TODO: write a comment.
		/// </summary>
		private void Finished ()
		{
//			if (OnFinished != null)
//				OnFinished(System.Threading.Thread.CurrentThread, this);
		}
		public override string ToString ()
		{
			return string.Format ("[ThreadWorkItem: description={0}, isGuiWork={1}, Work={2}]", description, isGuiWork, Work);
		}
		
	}
}
